package com.lanou.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.mapper.ConfigHouseMapper;
import com.lanou.mapper.PaymentContractMapper;
import com.lanou.pojo.PaymentContractEntity;
import com.lanou.service.PaymentContractService;
import com.lanou.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName PaymentContractServiceImpl
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月30日
 * @Version: 1.0
 */
@Service
public class PaymentContractServiceImpl implements PaymentContractService {
    @Autowired
    private PaymentContractMapper paymentContractMapper;

    @Autowired
    private ConfigHouseMapper configHouseMapper;

    @Override
    public String getPaymentContractList(String pageNum, String pageSize, String resourceName, String contractType, String userName) {

        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));

        List<PaymentContractEntity> paymentContractEntities = paymentContractMapper.selectPaymentContract(resourceName, contractType, userName);

        // 根据resourceId查询租金
        for (PaymentContractEntity p : paymentContractEntities) {
            String s = configHouseMapper.selectConfigHouseByResourceId(p.getResourceId());
            if (s != null ) {
                p.setRental(s);
            } else {
                p.setRental("");
            }
        }

        PageInfo<PaymentContractEntity> pageInfo = new PageInfo<>(paymentContractEntities);
        JsonResult<PageInfo<PaymentContractEntity>> jsonResult = new JsonResult<>();
        jsonResult.setData(pageInfo);
        jsonResult.setStatus(200);

        return JSON.toJSONString(jsonResult);
    }
}
